<template>
    <div>
        <!--  -->
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-calendar"></i> 医生管理
                </el-breadcrumb-item>
                <el-breadcrumb-item>新增医生</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <!--  -->
        <div class="container">
            <div class="form-box">
                <el-form ref="formRef" :rules="rules" :model="form" label-width="80px">
                    <el-form-item label="姓名" prop="doctorName">
                        <el-input v-model="form.doctorName"></el-input>
                    </el-form-item>
                    <el-form-item label="性别" prop="doctorGender">
                        <el-radio-group v-model="form.doctorGender">
                            <el-radio label="男" name="男"></el-radio>
                            <el-radio label="女" name="女"></el-radio>
                            <el-radio label="其他" name="其他"></el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="年龄" prop="doctorAge">
                        <el-input v-model="form.doctorAge" type="number"></el-input>
                    </el-form-item>
                    <el-form-item label="手机" prop="doctorPhone">
                        <el-input v-model="form.doctorPhone"></el-input>
                    </el-form-item>
                    <el-form-item label="科室" prop="doctorSectionOffice">
                        <el-input v-model="form.doctorSectionOffice"></el-input>
                    </el-form-item>
                    <el-form-item label="简介" prop="doctorIntro">
                        <el-input type="textarea" rows="5" v-model="form.doctorIntro"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit">新增人员</el-button>
                        <el-button @click="onReset">清空表单</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
import { reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import { addDoctor } from "../../api";
export default {
    name: "doctorForm",
    setup() {
        const rules = {
            doctorName: [
                { required: true, message: "请输入人员姓名", trigger: "blur" },
            ],
            doctorGender: [
                { required: true, message: "请选择人员性别", trigger: "blur" },
            ],
            doctorAge: [
                { required: true, message: "请输入人员年龄", trigger: "blur" },
            ],
            doctorPhone: [
                { required: true, message: "请输入人员手机", trigger: "blur" },
            ],
            doctorSectionOffice: [
                { required: true, message: "请输入人员科室", trigger: "blur" },
                // { type: 'number', message: '请输入合法的数字', trigger: "blur"  },
            ],
            doctorIntro: [
                { required: true, message: "请输入人员简介", trigger: "blur" },
            ],
        };
        const formRef = ref(null);

        // 表单数据
        let form = reactive({
            doctorName: '',
            doctorGender: '',
            doctorAge: null,
            doctorPhone: '',
            doctorSectionOffice: '',
            doctorIntro: '',
        });

        // 重置
        const onReset = () => {
            formRef.value.resetFields();
        };

        // 提交
        const onSubmit = () => {
            // 表单校验
            formRef.value.validate((valid) => {
                if (valid) {
                    console.log(form);
                    addDoctor(form).then(res => {
                        if (res.code === 200) {
                            // reload();
                            onReset();
                            ElMessage.success('添加成功');
                        } else {
                            console.log("[添加人员] 失败: " + res.message);
                            ElMessage.error('服务器繁忙，请稍后重试');
                        }
                    })
                } else {
                    return false;
                }
            });
        };

        return {
            rules,
            formRef,
            form,
            onSubmit,
            onReset,
        };
    },
};
</script>